package com.bestcem.xm.user.grpc.service.converter;

import com.bestcem.bp.xm.user.grpc.v1.services.Token;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.component.security.dto.TokenDTO;
import com.bestcem.xm.user.service.dto.user.AuthDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Date;
import java.util.Objects;

/**
 * grpc请求出入参转化类
 *
 * @author Linked <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/09 14:17
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserPermissionConverter {

    public static Token dtoToGrpcToken(AuthDTO authDTO) {
        Token.Builder builder = Token.newBuilder();
        if (StringUtils.isNotBlank(authDTO.getUserId())) {
            builder.setUserId(authDTO.getUserId());
        }
        if (StringUtils.isNotBlank(authDTO.getOrgId())) {
            builder.setOrgId(authDTO.getOrgId());
        }
        if (StringUtils.isNotBlank(authDTO.getOrgCode())) {
            builder.setOrgCode(authDTO.getOrgCode());
        }
        if (Objects.nonNull(authDTO.getExp())) {
            builder.setExpiredTime(GrpcDateUtil.getTimestamp(new Date(authDTO.getExp() * 1000)));
        }
        if (StringUtils.isNotBlank(authDTO.getRealName())) {
            builder.setName(authDTO.getRealName());
        }
        if (StringUtils.isNotBlank(authDTO.getAvatar())) {
            builder.setAvatar(authDTO.getAvatar());
        }
        if (Objects.nonNull(authDTO.getSuperRole())) {
            builder.setSuper(authDTO.getSuperRole().equals(NumberUtils.INTEGER_ONE));
        }
        if (Objects.nonNull(authDTO.getSenior())) {
            builder.setSenior(authDTO.getSenior().equals(NumberUtils.INTEGER_ONE));
        }
        return builder.build();


    }


    public static Token dtoToGrpcToken(TokenDTO tokenDTO) {
        Token.Builder builder = Token.newBuilder();
        if (StringUtils.isNotBlank(tokenDTO.getUserId())) {
            builder.setUserId(tokenDTO.getUserId());
        }
        if (StringUtils.isNotBlank(tokenDTO.getOrgId())) {
            builder.setOrgId(tokenDTO.getOrgId());
        }
        if (StringUtils.isNotBlank(tokenDTO.getOrgCode())) {
            builder.setOrgCode(tokenDTO.getOrgCode());
        }
        if (Objects.nonNull(tokenDTO.getExpiredTime())) {
            builder.setExpiredTime(GrpcDateUtil.getTimestamp(new Date(tokenDTO.getExpiredTime() * 1000)));
        }
        if (StringUtils.isNotBlank(tokenDTO.getRealName())) {
            builder.setName(tokenDTO.getRealName());
        }
        if (StringUtils.isNotBlank(tokenDTO.getAvatar())) {
            builder.setAvatar(tokenDTO.getAvatar());
        }
        if (Objects.nonNull(tokenDTO.getSuperRole())) {
            builder.setSuper(tokenDTO.getSuperRole().equals(NumberUtils.INTEGER_ONE));
        }
        if (Objects.nonNull(tokenDTO.getIsSenior())) {
            builder.setSenior(tokenDTO.getIsSenior().equals(NumberUtils.INTEGER_ONE));
        }
        return builder.build();


    }
}
